<?php 
$site_path = realpath(dirname(__FILE__));
define('__SITE_PATH', $site_path);

include("model/feedback.php");

$arr = explode("/", $_SERVER['PHP_SELF']);

$domain = substr($_SERVER['PHP_SELF'], 0, strlen($arr[1]) + 1);
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">window.jQuery || document.write('<script type="text/javascript" src="/win-skyexpress/js/jquery191.js"><\/script>')</script> 
    </head>
    <body>
        <form method="post" action="" class="cmxform" name="frmContact" id="frmContact" onsubmit="return contact_submit(this)">
            <div style="color:#333"></div>
            <ol>
                <li><label for="name">Họ Và Tên <em>*</em></label> 
                    <input name="name" id="name" value="" size="30"/></li>
                <li><label for="address">Địa Chỉ <em>*</em></label>
                    <input name="address" id="address" value="" size="30"/></li>
                <li><label for="phone">Số Điện Thoại <em>*</em></label> 
                    <input name="phone" id="phone" value="" size="30"/></li>
                <li><label for="comment">Nội Dung Liên Hệ</label> 
                    <textarea name="comment" id="comment" rows="8" cols="40"></textarea></li>
            </ol>					
            <p align="center"><input type="button" name="submit" id="button" style="width:100px" value="   Send   "></p>
        </form>
        <h1>View All</h1>
        <table>
            <tr>
                <td>Name</td>
                <td>Comment</td>
                <td>Phone</td>
            </tr>
            <?php
            $feedback = FeedBack::findAll();
            foreach ($feedback as $_feedback){  
                ?>
            <tr>
                <td><?php echo $_feedback["name"] ?></td>
                <td><?php echo $_feedback["comments"] ?></td>
                <td><?php echo $_feedback["phone"] ?></td>
            </tr>
            <?php } ?>
        </table>
        <script>
            $('#button').click(function() {
                var name = $('#name').val();
                var address = $('#address').val();
                var phone = $('#phone').val();
                var comment = $('#comment').val();

                if (name == '') {
                    alert('Họ tên không được để trống.');
                    return false;
                }
                if (address == '') {
                    alert('Địa chỉ không được để trống.');
                    return false;
                }
                if (phone == '') {
                    alert('Điện thoại không được để trống.');
                    return false;
                }
                if (comment == '') {
                    alert('Comment không được để trống.');
                    return false;
                } else {
                    $.ajax({
                        // The link we are accessing.
                        url: "<?php echo $domain ?>/ajax.php",
                        // The type of request.
                        type: "get",
                        data: "act=add_feedback&name=" + name + "&address=" + address + "&phone=" + phone + "&comment=" + comment,
                        // The type of data that is getting returned.
                        dataType: "html",
                        error: function() {
                            // Load the content in to the page.
                            $("#error").html("<label>Page Not Found!!</label>");
                            $("#error").css({display: "block"});
                        },
                        success: function(strData) {
                            // Load the content in to the page.
                            if (strData == "1") {
                                alert('Gửi phản hồi thành công.');
                            } else {
                                alert(strData);
                            }
                        }
                    });
                }
            });
        </script>
    </body>
</html>